<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <style>
      .box {
        width: 500px;
        height: 200px;
        margin: 200 auto;
        background-color: black;
      }
      .box1 {
        width: 100px;
        height: 100px;
        top: 80px;
        left: 30px;
        background-color: blue;
      }
      .box2 {
        width: 50px;
        height: 50px;
        background-color: skyblue;
        margin: 20px auto;
      }
      * {
        margin: 0;
        padding: 0;
      }
    </style>
    <div class="box">
      <div class="box1">
        <div class="box2"></div>
      </div>
    </div>
    <script>
      let box2 = document.querySelector(".box2");
      console.dir(box2);
      function boxRealPosition(dom) {
        if (!dom) return 0;
        let top = dom.offsetTop;
        let left = dom.offsetLeft;
        if (dom.offsetParent) {
          top += boxRealPosition(dom.offsetParent).top;
          left += boxRealPosition(dom.offsetParent).left;
        }
        return { top, left };
      }
      console.log(boxRealPosition(box2));
    </script>
  </body>
</html>
